#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Copyright 2025 Alain Carlucci, Silvano Seva

import struct
import sys

if len(sys.argv) != 3:
    print("Usage: %s <input_file.bin> <output_file.bin>" % (sys.argv[0], ))
    exit(-1)

key = [0x71, 0x21, 0x3c, 0x7f, 0x28, 0x7e, 0x2b, 0x7e, 0x77, 0x64, 0x7e, 0x7d,
       0x4e, 0x37, 0x7e, 0x1d, 0x5c, 0x44, 0x5e, 0x68, 0x67, 0x22, 0x51, 0x3a,
       0x66, 0x7d, 0x0b, 0x65, 0x77, 0x0c, 0x60, 0x7c, 0x33, 0x47, 0x5c, 0x61,
       0x65, 0x5a, 0x6b, 0x6a, 0x5d, 0x68, 0x59, 0x5d, 0x7a, 0x61, 0x0b, 0x59,
       0x31, 0x57, 0x6c, 0x56, 0x08, 0x4d, 0x6b, 0x4b, 0x54, 0x45, 0x50, 0x4e,
       0x53, 0x70, 0x3a, 0x6e, 0x2d, 0x42, 0x45, 0x6a, 0x48, 0x4d, 0x51, 0x37,
       0x54, 0x56, 0x44, 0x60, 0x37, 0x36, 0x7b, 0x57, 0x6d, 0x2e, 0x47, 0x16,
       0x00, 0x2a, 0x00, 0x3a, 0x62, 0x07, 0x01, 0x01, 0x03, 0x19, 0x78, 0x33,
       0x12, 0x28, 0x1a, 0x11, 0x09, 0x2b, 0x00, 0x13, 0x04, 0x1d, 0x22, 0x24,
       0x25, 0x4a, 0x4b, 0x1c, 0x27, 0x25, 0x03, 0x1e, 0x74, 0x01, 0x09, 0x75,
       0x1a, 0x19, 0x13, 0x7e, 0x16, 0x7c, 0x30, 0x09, 0x04, 0x76, 0x06, 0x3c,
       0x09, 0x0a, 0x0d, 0x7c, 0x0e, 0x00, 0x17, 0x36, 0x37, 0x02, 0x2b, 0x7a,
       0x6c, 0x0e, 0x00, 0x7b, 0x07, 0x2a, 0x05, 0x66, 0x79, 0x02, 0x03, 0x70,
       0x5e, 0x00, 0x72, 0x5a, 0x12, 0x3c, 0x10, 0x0f, 0x06, 0x0c, 0x75, 0x05,
       0x18, 0x14, 0x1c, 0x18, 0x0a, 0x08, 0x06, 0x7b, 0x7d, 0x2b, 0x76, 0x00,
       0x30, 0x67, 0x4a, 0x48, 0x0f, 0x6d, 0x73, 0x7b, 0x78, 0x1d, 0x00, 0x28,
       0x7a, 0x00, 0x31, 0x7d, 0x02, 0x73, 0x00, 0x3c, 0x0d, 0x01, 0x7d, 0x30,
       0x00, 0x39, 0x00, 0x7e, 0x27, 0x00, 0x1e, 0x78, 0x00, 0x2d, 0x10, 0x2e,
       0x30, 0x34, 0x39, 0x3a, 0x3c, 0x3d, 0x42, 0x41, 0x3f, 0x2b, 0x3d, 0x34,
       0x2e, 0x33, 0x30, 0x2d, 0x7d, 0x02, 0x64, 0x7e, 0x17, 0x6f, 0x3c, 0x20,
       0x04, 0x24, 0x20, 0x04, 0x6e, 0x03, 0x21, 0x01, 0x62, 0x00, 0x71, 0x00,
       0x30, 0x7e, 0x75, 0x3a]

input_file = sys.argv[1]
output_file = sys.argv[2]

file_data = open(input_file, "rb").read()
file_len = len(file_data) 

encoded_len = struct.pack("<I",  file_len )

payload = encoded_len + file_data + encoded_len + file_data
padlen = 8 # Magic number

# Append padding
payload = payload + (bytes([0] * padlen))

full_key = key * ((len(payload) + len(key) + 0x80) // len(key))

header1 = [
0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
]

# header_len at 0x14
header_len = struct.pack("<I",  len(payload) )

header2=[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00,

# 0x44
0x80, 0x00, 0x00, 0x00, 

0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 

# 0x6d      0x6f
0x01, 0x00, 0x19, 0x00, 

0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00
]

header = bytes(header1) + header_len + bytes(header2)

checksum_int = (sum(header + payload) % 65536) // 5
checksum_data = bytes([(checksum_int >> 8) & 0xff, checksum_int & 0xff])

encrypted_payload = bytes([x^y for x,y in zip(payload + checksum_data, full_key)])

open(output_file, "wb").write(header + encrypted_payload)
